<?php

namespace app\controller\admin;

use app\Code;
use app\controller\BaseController;
use app\dao\HonorDao;
use think\facade\Request;
use think\facade\Log;
use think\exception\ValidateException;
use app\validate\HonorVerifyValidate;

class HonorController extends BaseController
{
    // 审核荣誉申请
    public function verify($id)
    {
        $data = Request::put();

        // 参数验证
        try {
            validate(HonorVerifyValidate::class)->scene('verify')->check($data);
        } catch (ValidateException $e) {
            return $this->returnJson([],Code::UNPROCESSABLE_ENTITY, $e->getMessage());
        }

        // 权限校验
        $role = session('role');
        if ($role !== 'admin') {
            return $this->returnJson([],Code::FORBIDDEN,  '没有权限进行此操作');
        }

        try {
            $result = HonorDao::verifyHonor(
                $id,
                $data['status'],
                session('user_id')
            );


            if ($result) {
                Log::info("荣誉申请审核成功: {$id}");
                return $this->returnJson();
            }

            return $this->returnJson([],Code::BAD_REQUEST,  '审核失败');
        } catch (\Exception $e) {
            Log::error("审核荣誉失败: " . $e->getMessage());
            return $this->returnJson([],Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }
}
